package actor

import (
	"sync"
	"time"

	kadefine "kiwi/actor/define"
	kalog "kiwi/actor/log"
	katimer "kiwi/actor/timer"
	kawork "kiwi/actor/work"
	klog "kiwi/log"
)

type Base struct {
	pid          *kadefine.PID
	timerManager *katimer.Manager
}

func (b *Base) ActorID() string {
	return ""
}

func (b *Base) StartedWaitGroup() *sync.WaitGroup {
	return nil
}

func (b *Base) StoppedWaitGroup() *sync.WaitGroup {
	return nil
}

func (b *Base) MailBoxSize() int {
	return 0
}

func (b *Base) DeciderFunc() kadefine.DeciderFunc {
	return StopDecider
}

func (b *Base) FromRoot() bool {
	return true
}

// OnStarted 派生类必须调用基类的OnStarted，否则初始化不完整
func (b *Base) OnStarted(ctx kadefine.Context) {
	b.pid = ctx.Self()
	b.timerManager = katimer.CreateManager(ctx)
}

func (b *Base) OnStopping(ctx kadefine.Context) {

}

func (b *Base) OnStopped(ctx kadefine.Context) {

}

func (b *Base) OnRestarting(ctx kadefine.Context) {

}

func (b *Base) OnTerminate(ctx kadefine.Context, who *kadefine.PID, why kadefine.TerminatedReason) {

}

func (b *Base) BeforeTriggerTimer(id katimer.ID, tag katimer.Tag, ctx kadefine.Context) (bool, error) {
	return true, nil
}

func (b *Base) AfterTriggerTimer(err error, id katimer.ID, tag katimer.Tag, ctx kadefine.Context) {

}

func (b *Base) OnReceive(ctx kadefine.Context) {

}

func (b *Base) PID() *kadefine.PID {
	return b.pid
}

func (b *Base) Stop() {
	Stop(b.pid)
}

func (b *Base) Poison() {
	Poison(b.pid)
}

func (b *Base) Logger() klog.ILogger {
	return kalog.Get()
}

func (b *Base) Timer() *katimer.Manager {
	return b.timerManager
}

func (b *Base) Post(work kawork.IPostWork) error {
	return kawork.Post(RootContext(), b.pid, work)
}

func (b *Base) Dispatch(timeout time.Duration, work kawork.IDispatchWork) error {
	return kawork.Dispatch(RootContext(), b.pid, timeout, work)
}

func (b *Base) Send(message any) {
	Send(b.pid, message)
}

func (b *Base) RequestFuture(message any, timeout time.Duration) *kadefine.Future {
	return RequestFuture(b.pid, message, timeout)
}
